﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

namespace TetrisClient
{
    /// <summary>
    /// This class is responsible for updating the screen from time to time.
    /// </summary>
    public class UpdateScreenThread
    {
        private volatile bool shutdown = false;
        private volatile bool isInPause = false;
        private Thread thread;
        private TetrisPage tetrisPage;
        private int refreshTimeInMillis;

        /// <summary>
        /// Initializes a new instance of the <see cref="UpdateScreenThread"/> class.
        /// </summary>
        /// <param name="refreshTimeInMillis">The refresh time in millis.</param>
        /// <param name="tetrisPage">The tetris page.</param>
        public UpdateScreenThread(int refreshTimeInMillis, TetrisPage tetrisPage)
        {
            this.refreshTimeInMillis = refreshTimeInMillis;
            this.tetrisPage = tetrisPage;
            thread = new Thread(run);
        }

        /// <summary>
        /// Runs this thread.
        /// </summary>
        private void run()
        {
            while (!shutdown)
            {
                if (!isInPause)
                {
                    ThreadActionExecutor.execute(() => tetrisPage.moveDown());
                }
                Thread.Sleep(refreshTimeInMillis);
            }
        }

        /// <summary>
        /// Pauses the thread.
        /// </summary>
        public void pause()
        {
            isInPause = true;
        }

        /// <summary>
        /// Continues the thread.
        /// </summary>
        public void releasePause()
        {
            isInPause = false;
        }

        /// <summary>
        /// Determines whether the thread is paused.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance is paused; otherwise, <c>false</c>.
        /// </returns>
        public bool isPaused()
        {
            return isInPause;
        }

        /// <summary>
        /// Starts the thread.
        /// </summary>
        public void startThread()
        {
            thread.Start();
        }

        /// <summary>
        /// Stops the thread.
        /// </summary>
        public void stopThread()
        {
            shutdown = true;
        }

        /// <summary>
        /// Updates the refresh time.
        /// </summary>
        /// <param name="refreshTimeInMillis">The refresh time in millis.</param>
        public void updateRefreshTime(int refreshTimeInMillis)
        {
            if (refreshTimeInMillis > 0)
            {
                this.refreshTimeInMillis = refreshTimeInMillis;
            }
        }
    }
}
